<?php
$id=$_GET['id'];
$save=$_GET['save'];
$id_especialidad=$_GET['esp'];

require('./roots.php');
require($root_path.'include/inc_environment_global.php');

define('LANG_FILE','interconsulta.php');
define('NO_2LEVEL_CHK',1);
require_once($root_path.'include/inc_front_chain_lang.php');

$thisfile=basename(__FILE__);
$breakfile='interconsulta.php'.URL_APPEND;
$action="interconsulta-asignar-save.php".URL_APPEND."&id=".$id."&save=1";

require_once($root_path.'include/care_api_classes/class_core.php');
$core=new Core;

# Start Smarty templating here
/**
* LOAD Smarty
*/

# Note: it is advisable to load this after the inc_front_chain_lang.php so
# that the smarty script can use the user configured template theme
/*require_once($root_path.'include/care_api_classes/class_interconsulta.php');
$interconsulta = new Interconsulta;
//echo $id;
$temp = $interconsulta->getAllInfoObject($id);
//var_dump($temp);
if (!$save){
//echo "holaaa";
//var_dump($temp->_fecth());
//echo "Problema : ".$temp['ice_name'];
//extract($temp);
foreach($temp as $te){
//var_dump($te);	


$medicos = $interconsulta->loadMedicosEspecialidad($id_especialidad, $ice_name);
var_dump($medicos);
}
$medDispoCombo = '<select name="medicos"><option value="0">' . $LDSelect . '</option>';
if (count($medicos))
{
    while ($temp=$medicos->FetchRow())
    {
        $medDispoCombo .= '<option value="'. $te["id"] .'">' . $te["name"] . '</option>';
    }
}
$medDispoCombo .= '</select>';*/
require_once($root_path.'include/care_api_classes/class_interconsulta.php');
$interconsulta = new Interconsulta;
$temp = $interconsulta->getAllInfoObject($id);
//var_dump($temp);
if (!$save){
//var_dump( $id_especialidad);
$medicos = $interconsulta->loadMedicosEspecialidad($id_especialidad, $ice_name);
//var_dump($medicos);
$medDispoCombo = '<select name="medicos"><option value="0">' . $LDSelect . '</option>';
if (count($medicos))
{
    foreach ($medicos as $temp1)
    {
        $medDispoCombo .= '<option value="'. $temp1["id"] .'">' . $temp1["name"] . '</option>';
    }
}
$medDispoCombo .= '</select>';

require_once($root_path.'gui/smarty_template/smarty_care.class.php');
$smarty = new smarty_care('common');


# Collect extay javascript code
$sTemp='';
ob_start();
?>
<script  language="javascript">
	function chkform(d) {

		//Verificacion del combobox q tenga un medico seleccionado
		if(d.medicos.value != ""){
			if(d.medicos.value==0)
			{
				alert("<?php echo $LDMedicoError; ?>");
				return false;
			}
		}

		return true;
	}

</script>
<?php
$sTemp = ob_get_contents();
ob_end_clean();

$smarty->assign('sRegFormJavaScript',$sTemp);

$smarty->assign('action', $action);
$smarty->assign('sToolbarTitle',$LDAsignarInter);
$smarty->assign('title', $LDDetInter);

$smarty->assign('LDInterconsultante',$LDInterconsultante);
$smarty->assign('sInterconsultanteInput', $temp[0]['ice_name']);

$smarty->assign('LDEstablecimiento',$LDEstablecimiento);
$smarty->assign('sEstablecimientoInput', $temp[0]['establecimiento']);

$smarty->assign('LDEspecialidad',$LDEspecialidad);
$smarty->assign('sEpecialidadRadio',$temp[0]['especialidad_descripcion']);

$smarty->assign('LDPaciente',$LDCaseNr);
$smarty->assign('sPacienteInput',$temp[0]['id_paciente']);

$smarty->assign('LDNombreCompleto', $LDNombreCompleto);
$smarty->assign('sNombreCompletoInput',$temp[0]['apellido_1'].", ".$temp[0]['name_1']);

$smarty->assign('LDSexo',$LDSex);
$sCSBuffer='<input name="sex" type="radio" disabled ';
$sCSBuffer .= 'value="f" ';
if($temp[0]['sex']=="f") $sCSBuffer.='checked';
$smarty->assign('sFemInput',$sCSBuffer.'>');

$sCSBuffer='<input name="sex" type="radio" disabled ';
$sCSBuffer .= 'value="m" ';
if($temp[0]['sex']=="m") $sCSBuffer.='checked';
$smarty->assign('sMasInput',$sCSBuffer.'>');

$smarty->assign('LDFem',$LDFemenino);
$smarty->assign('LDMas',$LDMasculino);

$smarty->assign('LDInterData', $LDInterData);

$smarty->assign('LDFecha',$LDDate);
$smarty->assign('sFechaInput',$temp[0]['fecha_hora_consulta']);

$smarty->assign('LDMotivo',$LDMotive);
$smarty->assign('sMotivoText',$temp[0]['motivo']);

$listarIC ="<a href='../../modules/interconsulta/interconsulta-consultar.php".URL_APPEND."&id=".$id."&asig=1><b>".$LDMasDatosInter."</b></a>";
$smarty->assign('LDListarIC',$listarIC);


$smarty->assign('LDAsignarCons',$LDAsignarCons);

$smarty->assign('LDMedDispo',$LDMedDispo);
$smarty->assign('LDMedDispoCombo',$medDispoCombo);
$smarty->assign('pbSubmit','<input type="image" '.createLDImgSrc($root_path,'asignar-big.gif','0').'  alt="'.$LDAsignarInter.'" align="absmiddle">');

$smarty->assign('sMainBlockIncludeFile','interconsulta/save_asignar_interconsulta.tpl');

 /**
 * show Template
 */
 $smarty->display('common/mainframe.tpl');
}else{
	$interconsulta->asignarMedico($id,$_POST['medicos']);

        $login=$_SESSION['sess_login_userid'];

        $msj = $LDUsers.$login.$LDInterAsig. $id .$LDMedico. $_POST['medicos'];

	$interconsulta->setID($id);
        $interconsulta->insertarBitacora($msj);
	
	//Notificar al grupo de triaje
	sendMail($_POST['medicos']);


	header("Location:interconsulta-asignar.php".URL_REDIRECT_APPEND);
	exit;
}

function sendMail($login_medico) {
	require_once('./include/mail.php');

	$inter = new Interconsulta;
        $emails = $inter->getMail($login_medico);

	if($emails!=NULL){
		foreach($emails as $email){
			if($email != '') enviar_mail($email, $LDAsigInterSubject, $LDAsigInterBody);

		}
	}
}

?>
